package com.yf.exam.core.utils;

import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class Base64DecodedMultipartFile implements MultipartFile {

    private final byte[] imgContent;
    private final String fileName;
    private final String contentType;

    public Base64DecodedMultipartFile(byte[] imgContent, String fileName, String contentType) {
        this.imgContent = imgContent;
        this.fileName = fileName;
        this.contentType = contentType;
    }

    @Override public String getName() { return fileName; }
    @Override public String getOriginalFilename() { return fileName; }
    @Override public String getContentType() { return contentType; }
    @Override public boolean isEmpty() { return imgContent == null || imgContent.length == 0; }
    @Override public long getSize() { return imgContent.length; }
    @Override public byte[] getBytes() { return imgContent; }
    @Override public InputStream getInputStream() { return new ByteArrayInputStream(imgContent); }
    @Override public void transferTo(File dest) throws IOException, IllegalStateException {
        FileCopyUtils.copy(imgContent, dest);
    }
}
